package com.xxx.web.servlet;

import com.xxx.pojo.ClassInfo;
import com.xxx.service.Services;
import com.xxx.service.impl.ClassInfoServiceImpl;
import com.xxx.util.GetIdTime;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classInfo/*")
public class ClassInfoServlet extends BaseServlet{

    private Services service = new ClassInfoServiceImpl();
    private  Long id = GetIdTime.getId();

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<ClassInfo> classInfos = service.selectAll();
        req.setAttribute("classInfos",classInfos);
        System.out.printf("selectAll:ClassInfos");
        System.out.println(classInfos);
        req.getRequestDispatcher("/login/class.jsp").forward(req,resp);

    }

    public void addClassInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String types = req.getParameter("types");
        String typesName = req.getParameter("typesName");
        Long order = Long.valueOf(req.getParameter("order"));
        String createTime = GetIdTime.getDataTime();

        ClassInfo classInfo = new ClassInfo(types,typesName,order,createTime,createTime,this.id,this.id);

        int add = service.add(classInfo);
        selectAll(req, resp);

    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Long order = Long.valueOf(req.getParameter("order"));
        int delete = service.delete(order);
        selectAll(req, resp);
    }

    public void selectByOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Long order = Long.valueOf(req.getParameter("order"));
        ClassInfo classInfo = (ClassInfo)service.selectById(order);
        req.setAttribute("classInfo", classInfo);
        selectAll(req, resp);
    }

    public void updateByOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("UTF-8");

        Long order = Long.valueOf(req.getParameter("order"));
        String types = req.getParameter("types");
        String typesName = req.getParameter("typesName");
        String updateTime = GetIdTime.getDataTime();

        ClassInfo classInfo = new ClassInfo();

        classInfo.setOrder(order);
        classInfo.setTypes(types);
        classInfo.setTypesName(typesName);
        classInfo.setUpdateTime(updateTime);
        classInfo.setUpdateUser(this.id);

        int i = service.updateById(order);
        selectAll(req, resp);
    }
}
